package bit.pja.wolfkill.entity;

import bit.pja.wolfkill.aop.Flag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;


@Component
public class Game {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private String id;

    private Integer size;


    private List<User> users;

    private Integer flag;
    private int oldFlag;

    private List<Record> records = new ArrayList<>();
    private List<Record> oldRecords = new ArrayList<>();

    boolean prophetLive = true;
    boolean wizzardLive = true;
    boolean over = false;
    private int day = 1;

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

    public void setProphetLive(boolean prophetLive) {
        this.prophetLive = prophetLive;
    }

    public void setWizzardLive(boolean wizzardLive) {
        this.wizzardLive = wizzardLive;
    }

    public boolean isOver() {
        return over;
    }

    public void setOver(boolean over) {
        this.over = over;
    }


    public static final int FLAG_WOLF = 1;
    public static final int FLAG_PROPHET = 2;
    public static final int FLAG_WIZZARD = 3;
    public static final int FLAG_MORNING = 4;
    public static final int FLAG_VILLIGER = 5;
    public static final int FLAG_EVENING = 6;
    public static final int FLAG_HUNTER = 10;

    public static final int FLAG_BEGIN = 0;
    public static final int FLAG_END = FLAG_EVENING;


    public static final int FLAG_WOLF_WIN = 100;
    public static final int FLAG_WOLF_LOSE = 200;

    private int waitSecons = 10;

    public int getWaitSecons() {
        return waitSecons;
    }

    public void setWaitSecons(int waitSecons) {
        this.waitSecons = waitSecons;
    }



    private Date createdTime;

    private Date endTime;

    public Game() {

    }

    public Game(Room room) throws Exception{

        setId(UUID.randomUUID().toString().replaceAll("-", ""));
        this.size = room.getSize();
        List<User> users = room.getUsers();
        List<Role> roles = new ArrayList<>(room.getRoles());
        for(User user : users) {
            int index = new Random().nextInt(roles.size());
            user.setRole(roles.get(index));
            roles.remove(index);
        }
        setUsers(users);
        setCreatedTime(new Date());
        room.setGame(this);
        setFlag(Game.FLAG_BEGIN);
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Integer getSize() {
        return size;
    }

    public void setSize(Integer size) {
        this.size = size;
    }

    public List<User> getUsers() {
        return this.users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    public Integer getFlag() {
        return this.flag;
    }


    public List<Record> getOldRecords() {
        return oldRecords;
    }

    public void setOldRecords(List<Record> oldRecords) {
        this.oldRecords = oldRecords;
    }


    public void setFlag(Integer flag){
        this.flag = flag;
        switch (flag) {
            case FLAG_WOLF:
                logger.info("现在是阶段一，请狼人行动");
                break;
            case FLAG_PROPHET:
                logger.info("现在是阶段二，请预言家行动");
                break;
            case FLAG_WIZZARD:
                logger.info("现在是阶段三，请女巫行动");
                break;
            case FLAG_MORNING:
                logger.info("天亮了");
                return;
            case FLAG_VILLIGER:
                logger.info("请存活玩家投票");
                break;
            case FLAG_EVENING:
                logger.info("天黑了");
                return;
            case FLAG_HUNTER:
                logger.info("请猎人行动");
                break;
            case FLAG_WOLF_WIN:
                logger.info("狼人胜利");
                return;
            case FLAG_WOLF_LOSE:
                logger.info("人类胜利");
                return;
        }

    }

    public Date getCreatedTime() {
        return createdTime;
    }

    public void setCreatedTime(Date createdTime) {
        this.createdTime = createdTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public List<Record> getRecords() {
        return records;
    }

    public int getOldFlag() {
        return oldFlag;
    }

    public void setOldFlag(int oldFlag) {
        this.oldFlag = oldFlag;
    }

    public void setRecords(List<Record> records) {
        this.records = records;
    }

    public Record insertRecord(Record record){
        this.records.add(record);
        return record;
    }




}
